﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using SilverlightCalculator.CalculatorValidationRules;

namespace SilverlightCalculator
{
    [Export(typeof(ICalculatorValidator))]
    public class CalculatorValidator : ICalculatorValidator
    {
        [ImportMany]
        public IEnumerable<ICalculatorValidationRule> CalculatorValidationRules { get; set; }

        public ValidationResult Validate(string value)
        {
            List<ValidationResult> validationResults = new List<ValidationResult>();
            foreach (var calculatorValidationRule in CalculatorValidationRules)
            {
                if (!calculatorValidationRule.IsValid(value))
                {
                    return new ValidationResult()
                                {
                                    IsValid = false,
                                    ErrorMessage = calculatorValidationRule.ErrorMessage

                                };
                }
            }
            return new ValidationResult() { IsValid = true };
        }
    }

    public class ValidationResult
    {
        public bool IsValid { get; set; }
        public string ErrorMessage { get; set; }
    }
}